package com.zjhome.cloudnote.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zjhome.cloudnote.constant.ErrorCode;
import com.zjhome.cloudnote.domain.DirInfo;
import com.zjhome.cloudnote.exception.BaseException;
import com.zjhome.cloudnote.param.AckResult;
import com.zjhome.cloudnote.param.DirPagingParam;
import com.zjhome.cloudnote.param.DirPagingResult;
import com.zjhome.cloudnote.param.PagingResult;
import com.zjhome.cloudnote.service.DirService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 文件夹
 * 
 * @author zjhome
 *
 */
@RestController
@Api(value = "文件夹管理")
@RequestMapping("/dirmgr")
public class DirController extends BaseController {
	
	@Autowired
	private DirService	dirService;
	
	
	/**
	 * 文件夹列表查询
	 * 
	 * @param did	父文件夹id
	 * @return
	 * @throws Exception
	 */
//	@PostMapping("/list/{did}")
	@PostMapping("/list")
	@ApiOperation(value = "文件夹列表")
//	public List<DirInfo> getDirs( @PathVariable("did") String did  ) throws Exception {
//	public List<DirInfo> getDirs( @RequestParam(value = "did", required = false) String did ) throws Exception {
	public PagingResult<DirInfo> getDirs(DirPagingParam dirPagingParam) throws Exception {		
		String uid = this.getUserId();
		dirPagingParam.setUid(uid);
//		List<DirInfo> dirList = dirService.selectSubDirsByDid(did, uid);
		
		PagingResult<DirInfo> result = new PagingResult<DirInfo>();

		List<DirInfo> dirList = dirService.selectDirsByParaPaging(dirPagingParam);
		int total = dirService.selectDirsCountByPara(dirPagingParam);
			
		result.setTotal(total);
		result.setData(dirList);
		
		return result;
	}
	
	/**
	 * 获取文件夹信息
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "获取文件夹信息")
	@GetMapping(value = "/dir")
	public DirInfo getDirInfo(String id) throws Exception {
		if (StringUtils.isEmpty(id)) {
			throw new BaseException("", ErrorCode.ERR_INVALID_ID);
		}		
		
		String uid = this.getUserId();
		DirInfo dirInfo = dirService.selectDirById(id, uid);
		
		return dirInfo;		
	}
	
	/**
	 * 获取文件夹树
	 * 
	 * @param id	根目录id（不是必须）
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "获取文件夹树")
	@GetMapping(value = "/dirtree")
	public List<DirInfo> getDirTree(@RequestParam(value = "pid", required = false, defaultValue = "") String pid) throws Exception {
		
		String uid = this.getUserId();
		List<DirInfo> dirTree = dirService.selectDirTree(pid, uid);
		return dirTree;
	}
	

	/**
	 * 创建文件夹
	 * 
	 * @param dirInfo	文件信息
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/createdir")
//	public AckResult createDir(@RequestBody DirInfo dirInfo) throws Exception {		
	public AckResult createDir(DirInfo dirInfo) throws Exception {		
		dirInfo.setUid(this.getUserId());
		dirService.insertDirByParam(dirInfo);
		return new AckResult(ErrorCode.ERR_NORMAL);
	}	
	
	/**
	 * 更新文件夹信息 
	 * 
	 * @param dirInfo
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/updatedir")
	public AckResult updateDir(DirInfo dirInfo) throws Exception {
		
		if (StringUtils.isEmpty(dirInfo.getId())) {
			throw new BaseException("", ErrorCode.ERR_INVALID_ID);
		}
		
		dirInfo.setUid(this.getUserId());		
		dirService.updateDirByParam(dirInfo);
		
		return new AckResult(ErrorCode.ERR_NORMAL);
	}
	
		
	/**
	 * 删除文件夹
	 * 
	 * @param id	文件夹id
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/deldir")
	public AckResult delDir(String id) throws Exception {
		
		if (StringUtils.isEmpty(id)) {
			throw new BaseException("", ErrorCode.ERR_INVALID_ID);
		}
		
		dirService.deleteDirById(id, this.getUserId());
		
		return new AckResult(ErrorCode.ERR_NORMAL);
	}
	
}
